#include "StdAfx.h"
#include "Tinylogger.h"

CTinyLogger::CTinyLogger(void)
{
	//get programme path
	char szFile[MAX_PATH];
	char szPath[MAX_PATH];
	GetModuleFileNameA(NULL,szFile,MAX_PATH);
	size_t i;
	for(i=strlen(szFile)-1; i>0 && szFile[i]!='\\'; i--);
	szFile[i]='\0';
	strcpy(szPath,szFile);
	//end get

	//string trace = "fa";
	string Property = std::string(szPath)+"\\log4cxx.properties";
    log4cxx::PropertyConfigurator::configure(Property);
    //m_logger = Logger::getLogger(trace);
	//m_logger->info("TinyLoggerStarting at ....."+std::string(szPath));
}

CTinyLogger::~CTinyLogger(void)
{
}

void CTinyLogger::selectlogType(std::string type)
{
	  m_logger = Logger::getLogger(type);
	  m_logger->info("TinyLoggerStarting at .....");
}

void CTinyLogger::log(ENUM_LOGLEVLE level,std::string log )
{
	switch(level)
	{
	case debug1:
		m_logger->debug(log);
		break;
	case debug2:
		m_logger->debug(log);
		break;
	case info:
		m_logger->info(log);
		break;
	case error:
		m_logger->error(log);
		break;
	case fatal:
		m_logger->fatal(log);
		break;
	default:
		m_logger->info("Unknown msg:"+log);
		break;
	}
}
void CTinyLogger::log(ENUM_LOGLEVLE level,CTinyMsg& msg)
{
	if(msg.getMsgIndex()>0&&msg.getMsgItem()>=0)
	{
		char buff[1024];
		sprintf_s(buff,1024,"Msg:Index = %d, Item = %d, SourceIP = %s, SourcePort = %d, SocketID = %d",
			msg.getMsgIndex(),msg.getMsgItem(),msg.getSourceIP(),msg.getSourcePort(),msg.getSocketID());
		m_logger->debug(buff);
	}
}